package listbyorder.access101_200.test140;

import java.util.ArrayList;
import java.util.List;

/**
 * 单词拆分
 *
 * @author code_yc
 * @version 1.0
 * @date 2020/6/11 14:13
 */
public class Solution1 {

    // 暴力递归
    public List<String> wordBreak(String s, List<String> wordDict) {
        List<String> res = new ArrayList<>();
        dfs(res, s, wordDict, 0, new StringBuilder());
        return res;
    }

    private void dfs(List<String> res, String s, List<String> wordDict, int c, StringBuilder path) {
        if (c == s.length()) {
            res.add(new String(path.deleteCharAt(path.length() - 1)));
            return;
        }

        String before = new String(path);
        for (int i = c; i < s.length(); i++) {
            String cur = s.substring(c, i + 1);
            if (wordDict.contains(cur)) {
                path = new StringBuilder(before);
                path.append(cur + " ");
                dfs(res, s, wordDict, i + 1, path);
            }
        }
    }
}
